﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Saturn.App.Common;
using Saturn.App.FormOper;

namespace Saturn.App
{
    public partial class FormBillSearchs : Form
    {
        DataTable billCodeTable;
        public FormBillSearchs()
        {
            InitializeComponent();
        }

        private void lvwBillCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwBillCode.SelectedIndices.Count > 0)
            {
                ListViewItem item = lvwBillCode.FocusedItem;
                if (item == null) return;

                txtBillCode.Text = item.Text;
                txtBillCode.Focus();
            }
        }

        private void txtBillCode_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtBillCode.Text)) return;
                DataRow[] drs = billCodeTable.Select("BillCode like '%" + txtBillCode.Text + "%'");
                if (drs.Length == 0)
                {
                    lvwBillCode.Items.Clear();
                    return;
                }
                DataTable newTable = billCodeTable.Copy();
                newTable.Rows.Clear();
                foreach (DataRow dr in drs)
                {
                    newTable.ImportRow(dr);
                }

                lvwBillCode.Items.Clear();

                foreach (DataRow dr in newTable.Rows)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = dr["BillCode"].ToString();
                    if (string.Equals(dr["BillSatus"].ToString(), "3"))
                        item.ForeColor = Color.Red;
                    lvwBillCode.Items.Add(item);

                }
            }
            catch { }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtBillCode.Text))
            {
                CommonSound.MessageBeep(CommonSound.Information);
                MessageBox.Show("请先在表格中选择单号！", "Info");
                return;
            }

            //if (billTypeEnum == BillTypeEnum.ProductIn)
            //    billType = CKORRK.RK.ToString();
            //else
            //    billType = CKORRK.CK.ToString();

            BillMainOper BMOper = new BillMainOper();
            BMOper.BillCode = txtBillCode.Text;
            //BMOper.BillType = billType;
            //BMOper.billTypeEnum = billTypeEnum;
            //BMOper.BillSubType = ComMethod.GetBillSubType(billTypeEnum);
            BMOper.SendOrgCode = UserLogin.MainOrgID;
            BMOper.SenderCode = UserLogin.userID;
            //FormBillAdd billaddform = new FormBillAdd(BMOper);
            //billaddform.ShowDialog();

            //txtBillCode.Focus();
            //txtBillCode.Text = string.Empty;

            //loadBillCode();
        }

        private void btnFunction_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmBillOnLine_Load(object sender, EventArgs e)
        {
            try
            {
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.BackColor = Color.SeaShell;
                txtBillCode.Text = string.Empty;
                txtBillCode.Focus();

                string loadResult = loadBillCode();
                if (!string.Equals(loadResult, "OK"))
                {
                    CommonSound.MessageBeep(CommonSound.Information);
                    MessageBox.Show(loadResult, "Info");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示信息");
            }
        }

        /// <summary>
        /// 单据加载
        /// </summary>
        private string loadBillCode()
        {
            string message = "OK";
            DataSet returnData = new DataSet();
            SaturnWebReference.SaturnWebService sop = new Saturn.App.SaturnWebReference.SaturnWebService();
            try
            {
                //returnData = sop.GetBillCode(UserLogin.userID, "");
            }
            catch (Exception ex)
            {
                message = ex.ToString() + "网络异常！";
                return message;
            }

            if (returnData == null || returnData.Tables.Count == 0) return message;

            billCodeTable = new DataTable();
            billCodeTable = returnData.Tables[0];
            lvwBillCode.Items.Clear();
            List<string> billOKLst = new List<string>();

            foreach (DataRow dr in billCodeTable.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.Text = dr["BillCode"].ToString();

                if (!string.Equals(dr["BillSatus"].ToString(), "0"))
                {
                    item.ForeColor = Color.Red;
                }
                lvwBillCode.Items.Add(item);
            }
            return message;
        }

    }
}